﻿using KTD.Web.Models;
using KTD.Web.Models.VD;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XXS.Common.BaseDataAccessLayer;
using XXS.Common.Paginate;

namespace KTD.Web.BLL
{
    public class AdvManager
    {

        private readonly IBaseDAL<Adv> _AdvDal;

        private readonly IBaseDAL<House> _HouseDal;
        private readonly IBaseDAL<Area> _AreaDal;
        public AdvManager()
        {
            _AdvDal = new BaseDAL<Adv>(new DB());
            _HouseDal = new BaseDAL<House>(new DB());
            _AreaDal = new BaseDAL<Area>(new DB());
        }

        /// <summary>
        /// 获取广告列表
        /// </summary>
        /// <param name="im"></param>
        /// <returns></returns>
        public virtual IPaginate<AdvVD> GetListBy(AdvQD im)
        {
            var res = _AdvDal.FindList();
            if (!string.IsNullOrEmpty(im.Type))
            {
                res = res.Where(n => n.type == im.Type);
            }
            if (im.CityId!=null)
            {
                res = res.Where(n => n.CityId == im.CityId);
            }
            if (!string.IsNullOrEmpty(im.Query))
            {
                res = res.Where(n => n.advname.Contains(im.Query) || n.query.Contains(im.Query));
            }
            var list = res.OrderBy(n => n.displayorder)
                .ToPaginate(im, n => new AdvVD().MapFrom(n));
            return list;
        }

        /// <summary>
        /// 新建广告
        /// </summary>
        /// <param name="im"></param>
        public void Create(AdvCUD im)
        {
            Adv model = new Adv();
           

            im.MapTo(model);
            if (!string.IsNullOrEmpty(im.advname))
            {
                var house = _HouseDal.FindList(n => n.housename == im.advname).FirstOrDefault();
                if (house != null)
                {
                    model.appid = house.Id.ToString();
                }

            }

            if (im.CityId!=null)
            {
                var area = _AreaDal.Find(im.CityId);
                if (area != null)
                {
                    model.url = area.name.ToString();
                }

            }
            _AdvDal.Create(model);
        }

        /// <summary>
        /// 更新广告
        /// </summary>
        /// <param name="im"></param>
        public void Update(AdvCUD im)
        {

            Adv model = _AdvDal.Find(im.Id);
            var thumb = model.thumb;
            if (model == null)
            {
                throw new Exception("错误的ID");
            }
            im.MapTo(model);
            if (string.IsNullOrEmpty(im.thumb))
            {
                model.thumb = thumb;
            }
            if (!string.IsNullOrEmpty(im.advname))
            {
                var house = _HouseDal.FindList(n => n.housename == im.advname).FirstOrDefault();
                if (house != null)
                {
                    model.appid = house.Id.ToString();
                }
                
            }
            if (im.CityId != null)
            {
                var area = _AreaDal.Find(im.CityId);
                if (area != null)
                {
                    model.url = area.name.ToString();
                }

            }
            _AdvDal.Update(model);
        }
        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AdvCUD Find(int id)
        {
            Adv model = _AdvDal.Find(id);
            return new AdvCUD().MapFrom(model);
        }

        public AdvCUD FindByName(string name)
        {
            Adv model = _AdvDal.Find(n=>n.advname==name);
            return new AdvCUD().MapFrom(model);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            Adv model = _AdvDal.Find(id);
            if (model == null)
            {
                throw new Exception("错误的ID");
            }
            _AdvDal.Delete(model);
        }

        public void DeleteImg(int id)
        {
            Adv model = _AdvDal.Find(id);
            if (model == null)
            {
                throw new Exception("错误的ID");
            }
            model.thumb = null;
            _AdvDal.Update(model);
        }
    }
}